package com.cn.web.controller.sys;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.StatusOperationDto;
import com.cn.bean.dto.SystemParametersDto;
import com.cn.bean.pojo.sys.BusinessStateOperationSub_Table;
import com.cn.bean.pojo.sys.BusinessStatusOperationMainTable;
import com.cn.bean.pojo.sys.SystemParameterSub_Table;
import com.cn.bean.pojo.sys.SystemParametersMainTable;
import com.cn.config.config.Result;
import com.cn.service.server.sys.StatusOperationService;
import com.cn.service.server.sys.SystemParametersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
public class SystemParametersController {
    @Autowired
    private SystemParametersService systemParametersService;

    @RequestMapping("/select/systemParameters")
    @ResponseBody
    public List<SystemParametersDto> selectSystemParameters(@RequestBody SystemParametersDto systemParametersDto) {
        List<SystemParametersDto> assists = systemParametersService.selectSystemParameters(systemParametersDto);
        return assists;
    }
    @RequestMapping("/select/systemParametersAll")
    @ResponseBody
    public List<SystemParametersDto> selectSystemParametersAll(@RequestBody SystemParametersDto systemParametersDto) {
        List<SystemParametersDto> systemParameters = systemParametersService.selectAll(systemParametersDto);
        return systemParameters;
    }
    @RequestMapping("/insert/systemParameters")
    @ResponseBody
    public Result insertSystemParameters(@RequestBody JSONObject jsonObject) {

        JSONArray dataArray = jsonObject.getJSONArray("requestData");

        SystemParametersMainTable mainTable = new SystemParametersMainTable();
        Integer id = systemParametersService.getSystemParametersM();
        id = id != null ? ++id : 1;
        String tableName = jsonObject.getString("tableName");
        String field = jsonObject.getString("field");
        int status=jsonObject.getInteger("status");
        mainTable.setId(id);
        mainTable.setSequenceNumber(id.toString());
        mainTable.setField(field);
        mainTable.setTableName(tableName);
        mainTable.setStatus(status);
        int a = systemParametersService.insertSystemParametersMain(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<SystemParametersDto> djBhList = dataArray.toJavaList(SystemParametersDto.class);
            for (SystemParametersDto dataStructureDto : djBhList) {
                SystemParameterSub_Table subTable = new SystemParameterSub_Table();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = systemParametersService.getSystemParametersSub();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                subTable.setSequenceNumber(autoid.toString());
                int b = systemParametersService.insertSystemParametersSub(subTable);
                if (b != 1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }
    @RequestMapping("/del/systemParameters")
    @ResponseBody
    public Result delSystemParameters(@RequestBody SystemParametersDto systemParametersDto) {
        int id=systemParametersDto.getId();
        List<SystemParametersDto> dataStructureDtos=systemParametersService.selectSub(id);
        int c=systemParametersService.delSystemParametersMain(id);
        if (dataStructureDtos.size()>0){
            int d=systemParametersService.delSystemParametersSub(id);
        }
        if (c!=1){
            return Result.failure("500");
        }
        return Result.success(200);
    }
    @RequestMapping("/update/systemParameters")
    @ResponseBody
    public Result updateSystemParameters(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        SystemParametersMainTable mainTable = new SystemParametersMainTable();
        int id = jsonObject.getInteger("id");
        String sequenceNumber=jsonObject.getString("sequenceNumber");
        String tableName = jsonObject.getString("tableName");
        String field = jsonObject.getString("field");
        int status=jsonObject.getInteger("status");
        mainTable.setId(id);
        mainTable.setSequenceNumber(sequenceNumber);
        mainTable.setField(field);
        mainTable.setTableName(tableName);
        mainTable.setStatus(status);
        mainTable.setTableName(tableName);
        int a = systemParametersService.updateSystemParametersM(mainTable);
        if (a != 1) {
            return Result.failure("500","更新主表失败");
        }
        List<SystemParametersDto> statusOperationDtos=systemParametersService.selectSub(id);
        if (statusOperationDtos.size()>0){
            int sub = systemParametersService.delSystemParametersSub(id);
        }
        if (dataArray==null||dataArray.isEmpty()){
            return Result.success(200);
        }
        List<SystemParameterSub_Table> djBhList = dataArray.toJavaList(SystemParameterSub_Table.class);
        for (SystemParameterSub_Table dataStructureDto : djBhList) {
            SystemParameterSub_Table subTable = new SystemParameterSub_Table();
            BeanUtils.copyProperties(dataStructureDto, subTable);
            Integer autoid = systemParametersService.getSystemParametersSub();
            autoid = autoid != null ? ++autoid : 1;
            subTable.setId(autoid);
            subTable.setMid(id);
            subTable.setSequenceNumber(autoid.toString());
            int b = systemParametersService.insertSystemParametersSub(subTable);
            if (b != 1) {
                return Result.failure("500","更新子表失败");
            }
        }
        return Result.success(200);
    }
}
